package repositories;

import java.util.Collection;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import domain.PurchasedItem;

@Repository
public interface PurchasedItemRepository extends
		JpaRepository<PurchasedItem, Integer> {

	@Query("select p.seller.id, sum(p.price.amount*p.purchasedUnits) from PurchasedItem p group by p.seller.id")
	Collection<Object[]> findMoreEarnedBySeller();

	@Query("select pi from PurchasedItem pi where pi.seller.id=?1")
	Collection<PurchasedItem> findPurchasedItemsBySellerId(int sellerId);

	@Query("select pi.referenceNumber from PurchasedItem pi where pi.order.buyer.id=?1")
	Collection<String> findRefNumbersOfMyPurchasedItems(int buyerId);

	@Query("select pi from PurchasedItem pi where pi.order.id = ?1")
	Collection<PurchasedItem> findByOrderId(int orderId);

	@Query("select pi from PurchasedItem pi where pi.order.buyer.id=?1")
	Collection<PurchasedItem> findPurchasedItemsByBuyerId(int buyerId);
	
	@Query("select pi from PurchasedItem pi where pi.referenceNumber=?1")
	Collection<PurchasedItem> findPurchasedItemIdByAvilableItemRefNumber(String ref);

}
